//
//  PhotoBroswerVC.m
//  CorePhotoBroswer
//
//  Created by 成林 on 15/5/4.
//  Copyright (c) 2015年 冯成林. All rights reserved.
//

#import "PhotoBroswerVC.h"
#import "PhotoBroswerLayout.h"
#import "PhotoItemView.h"
#import "UIView+Extend.h"
#import "UIImage+Extend.h"
#import "PBConst.h"
#import "CoreSVP.h"
#import "CoreArchive.h"
#import "PBScrollView.h"
#import "CALayer+Transition.h"
#import "ProductDetailsLabels.h"


@interface PhotoBroswerVC ()<UIScrollViewDelegate,FactoryOrderingDelegate>





/** scrollView */
@property (weak, nonatomic) IBOutlet PBScrollView *scrollView;

/** 库存*/
@property(strong,nonatomic)UIButton *kucunBtn;
@property(strong,nonatomic)UIButton *xiadanBtn;
@property(strong,nonatomic)UIButton *listBtn;
@property(strong,nonatomic)UIButton *factotyBtn;

@property(strong,nonatomic)ProductDetailsLabels *labels;
@property(strong,nonatomic)ProductDetailsLabels *rightLabels;


/** 初始显示的index */
@property (nonatomic,assign) NSUInteger index;
/** 相册数组 */
@property (nonatomic,strong) NSArray *photoModels;
/** 外部操作控制器 */
@property (nonatomic,weak) UIViewController *handleVC;
/** 类型 */
@property (nonatomic,assign) PhotoBroswerVCType type;
/** 展示的数据*/
@property(nonatomic,assign) NSMutableArray *productList;



@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scrollViewRightMarginC;






/** 总页数 */
@property (nonatomic,assign) NSUInteger pageCount;

/** page */
@property (nonatomic,assign) NSUInteger page;

/** currPage */
@property long currPage;


/** 上一个页码 */
@property (nonatomic,assign) NSUInteger lastPage;





/** 可重用集合 */
@property (nonatomic,strong) NSMutableSet *reusablePhotoItemViewSetM;

/** 显示中视图字典 */
@property (nonatomic,strong) NSMutableDictionary *visiblePhotoItemViewDictM;

/** 要显示的下一页 */
@property (nonatomic,assign) NSUInteger nextPage;

/** drag时的page */
@property (nonatomic,assign) NSUInteger dragPage;

/** 当前显示中的itemView */
@property (nonatomic,weak) PhotoItemView *currentItemView;

@end

@implementation PhotoBroswerVC
@synthesize kucunBtn,labels,rightLabels,xiadanBtn,factotyBtn,listBtn;



+(void)show:(UIViewController *)handleVC type:(PhotoBroswerVCType)type index:(NSUInteger)index photoModelBlock:(NSArray *(^)())photoModelBlock{
   
    
    //取出相册数组
    NSArray *photoModels = photoModelBlock();
    
    if(photoModels == nil || photoModels.count == 0) return;
    
    NSString *result= [PhotoModel check:photoModels type:type];
    
    if(result !=nil){
        NSLog(@"%@",result);
        return;
    }
    
    
    
    PhotoBroswerVC *pbVC = [[self alloc] init];
    
    if(index >= photoModels.count){
        NSLog(@"错误：index越界！");
        return;
    }
    
    //记录
    pbVC.index = index;
    
    pbVC.photoModels = photoModels;
    
    //记录
    pbVC.type =type;
    
    pbVC.handleVC = handleVC;
    
    pbVC.currPage = index;
    
   
    
    //展示
    [pbVC show];
}


+(void)show:(UIViewController *)handleVC type:(PhotoBroswerVCType)type showData:(NSMutableArray *)data index:(NSUInteger)index photoModelBlock:(NSArray *(^)())photoModelBlock
{
    //取出相册数组
    NSArray *photoModels = photoModelBlock();
    
    if(photoModels == nil || photoModels.count == 0) return;
    
    NSString *result= [PhotoModel check:photoModels type:type];
    
    if(result !=nil){
        NSLog(@"%@",result);
        return;
    }
    
    
    
    PhotoBroswerVC *pbVC = [[self alloc] init];
    
    if(index >= photoModels.count){
        NSLog(@"错误：index越界！");
        return;
    }
    
    //记录
    pbVC.index = index;
    
    pbVC.photoModels = photoModels;
    
    //记录
    pbVC.type =type;
    
    pbVC.handleVC = handleVC;
    
    pbVC.productList = data;
    
    pbVC.currPage = index;
    
    //展示
    [pbVC show];
}


/** 真正展示 */
-(void)show{
    
    switch (_type) {
        case PhotoBroswerVCTypePush://push
            
            [self pushPhotoVC];
            
            break;
        case PhotoBroswerVCTypeModal://modal
            
            [self modalPhotoVC];
            
            break;
            
        case PhotoBroswerVCTypeTransition://transition
            
            [self transitionPhotoVC];
            
            break;
            
        case PhotoBroswerVCTypeZoom://zoom
            
            [self zoomPhotoVC];
            
            break;
            
        default:
            break;
    }
}


/** push */
-(void)pushPhotoVC{
    
    [_handleVC.navigationController pushViewController:self animated:YES];
}


/** modal */
-(void)modalPhotoVC{
    
    [_handleVC presentViewController:self animated:YES completion:nil];
}


/** transition */
-(void)transitionPhotoVC{
    [_handleVC.navigationController pushViewController:self animated:NO];
    [_handleVC.navigationController.view.layer transitionWithAnimType:TransitionAnimTypePageCurl subType:TransitionSubtypesFromRamdom curve:TransitionCurveRamdom duration:0.7f];
}


/** zoom */
-(void)zoomPhotoVC{
    
    
    //拿到window
    UIWindow *window = _handleVC.view.window;
    
    if(window == nil){
        
        NSLog(@"错误：窗口为空！");
        return;
    }
    
    PhotoModel *photoModel = self.photoModels[self.index];
    
    photoModel.sourceImageView.hidden = YES;
    
    self.view.frame=[UIScreen mainScreen].bounds;
    
    //添加视图
    [window addSubview:self.view];
    
    //添加子控制器
    [_handleVC addChildViewController:self];
    
   // self.topBarView.alpha=0;
    
//    [UIView animateWithDuration:.25f animations:^{
//        self.topBarView.alpha=1;
//    } completion:^(BOOL finished) {
//        photoModel.sourceImageView.hidden = NO;
//    }];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.6f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.view.backgroundColor = [UIColor blackColor];
    });
}



- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self setupLeftTableView];
    [self setupRightTableView];
    
    // btnfasongtupian_up
    UIButton *fasongBtn  = [[UIButton alloc] initWithFrame:CGRectMake(marginSize, SCREEN_HEIGHT - 60, 100, 40)];
    [fasongBtn setImage:[UIImage imageNamed:@"fasongtupian_1"] forState:UIControlStateNormal];
    [fasongBtn setImage:[UIImage imageNamed:@"fasongtupian_2"] forState:UIControlStateHighlighted];
    [self.view addSubview:fasongBtn];
    
    [fasongBtn bk_whenTapped:^{
        [UMSocialData defaultData].extConfig.wxMessageType = UMSocialWXMessageTypeImage;
        [UMSocialSnsService presentSnsIconSheetView:self
                                             appKey:UM_KEY
                                          shareText:nil
                                         shareImage:[ToolUtils snapshot:self.currentItemView]
                                    shareToSnsNames:@[UMShareToWechatSession,UMShareToWechatTimeline]
                                           delegate:nil];

    }];
    //控制器准备
    [self vcPrepare];
}

-(void)setupRightTableView
{
    ProductModel *model = [self.productList objectAtIndex:self.currPage];


    NSDictionary *sizeColor=[AppService getSizeAndColorByModel:model.modelNo];
    NSArray *colorList=[sizeColor objectForKey:@"color"];
    NSArray *sizeList=[sizeColor objectForKey:@"size"];
    
    int width = 320.0/([sizeList count]+1);
    int defaultColumnsWidth = 80;
    defaultColumnsWidth = width>defaultColumnsWidth? defaultColumnsWidth:width;
    
    NSMutableArray *colums = [NSMutableArray array];
    for (int i=0;i<[sizeList count];i++) {
        [colums addObject:[NSString stringWithFormat:@"%d",defaultColumnsWidth]];
    }
    [colums addObject:[NSString stringWithFormat:@"%d",defaultColumnsWidth]];
    
    
    rightLabels = [[ProductDetailsLabels alloc]
              initWithFrame:CGRectMake(SCREEN_WIDTH - colums.count*defaultColumnsWidth - marginSize, CGRectGetMaxY(_backBtn.frame)+marginSize*2, 100, 40) andColumnsWidths:colums];
    [rightLabels addTopView:model.modelNo];
    NSMutableArray *titleColums = [NSMutableArray array];
    for (int i=0;i<[sizeList count];i++) {
        NSString *model = [sizeList objectAtIndex:i];
        if(i==0)
        {
            [titleColums addObject:@"颜色"];
        }
        [titleColums addObject:model];
    }
    [rightLabels addRecord:titleColums];
    

    int totalNumber = 0;
    for (int i=0; i<colorList.count; i++) {
        NSMutableArray *numberColums = [NSMutableArray array];
        NSString *colormodel = [colorList objectAtIndex:i];
        [numberColums addObject:colormodel];
        for (int j=0; j<sizeList.count; j++) {
            NSString *size = [sizeList objectAtIndex:j];
//            FactoryProductInfoModel *fmodel = [FactoryProductInfoModel searchSingleWithWhere:@{@"productId":[ToolUtils numberToStr:model.productId],@"sizeName":size,@"colorName":colormodel} orderBy:nil];
//            [numberColums addObject:[NSString stringWithFormat:@"%d",fmodel.factoryProductNumber]];
//            totalNumber+=fmodel.factoryProductNumber;
            int countt =0;
            NSMutableArray *fmodels = [FactoryProductInfoModel searchWithWhere:@{@"productId":[ToolUtils numberToStr:model.productId],@"sizeName":size,@"colorName":colormodel} orderBy:nil offset:0 count:0];
            for (int i=0; i<fmodels.count; i++) {
                FactoryProductInfoModel *fmodel  = fmodels[i];
                 totalNumber+=fmodel.factoryProductNumber;
                countt +=fmodel.factoryProductNumber;
            }
            
             [numberColums addObject:[NSString stringWithFormat:@"%d",countt]];
            
        }
        
        
        [rightLabels addRecord:numberColums];
        
    }
    
    
    [rightLabels addBootomView:[NSString stringWithFormat:@"￥ %0.1f 预:%d",model.salePrice,totalNumber]];
    [self.view addSubview:rightLabels];
    
    
        xiadanBtn  = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-110, CGRectGetMaxY(rightLabels.frame)+marginSize*2, 100, 40)];
        [xiadanBtn setImage:[UIImage imageNamed:@"xiadan_1"] forState:UIControlStateNormal];
        [xiadanBtn setImage:[UIImage imageNamed:@"xiadan_2"] forState:UIControlStateHighlighted];
        [self.view addSubview:xiadanBtn];
    
    
        listBtn  = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-110, CGRectGetMaxY(xiadanBtn.frame)+marginSize, 100, 40)];
        [listBtn setImage:[UIImage imageNamed:@"qingdan_1"] forState:UIControlStateNormal];
        [listBtn setImage:[UIImage imageNamed:@"qingdan_2"] forState:UIControlStateHighlighted];
        [self.view addSubview:listBtn];
    
        factotyBtn  = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-110, CGRectGetMaxY(listBtn.frame)+marginSize*2, 100, 40)];
        [factotyBtn setImage:[UIImage imageNamed:@"gongchang_1"] forState:UIControlStateNormal];
        [factotyBtn setImage:[UIImage imageNamed:@"gongchang_2"] forState:UIControlStateSelected];
        [self.view addSubview:factotyBtn];
    
        [factotyBtn bk_whenTapped:^{
            factotyBtn.selected = !factotyBtn.selected;
    
            if(factotyBtn.selected)
            {
                xiadanBtn.hidden = YES;
                listBtn.hidden = YES;
                rightLabels.hidden = YES;
                 factotyBtn.frame = CGRectMake(factotyBtn.frame.origin.x, CGRectGetMaxY(_backBtn.frame)+marginSize*2, factotyBtn.frame.size.width,  factotyBtn.frame.size.height);
            }else
            {
                xiadanBtn.hidden = NO;
                listBtn.hidden = NO;
                rightLabels.hidden = NO;
                 factotyBtn.frame = CGRectMake(factotyBtn.frame.origin.x,CGRectGetMaxY(listBtn.frame)+marginSize*2, factotyBtn.frame.size.width,  factotyBtn.frame.size.height);
            }
        }];
    
        [xiadanBtn bk_whenTapped:^{
            ProductModel *model = [self.productList objectAtIndex:self.currPage];
            FactoryOrderingViewController *factoryOrderingVC = [[FactoryOrderingViewController alloc] init];
            factoryOrderingVC.delegate = self;
            factoryOrderingVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;
            factoryOrderingVC.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;
            self.definesPresentationContext = YES;
            factoryOrderingVC.paramsModelNo = model.modelNo;
            [self presentViewController:factoryOrderingVC animated:YES completion:nil];
        }];
    
        [listBtn bk_whenTapped:^{
            FactoryListViewController *menuVc=[[FactoryListViewController alloc]init];
            menuVc.paramsModeNo = model.modelNo;
            menuVc.baseCol = self;
            UIPopoverController *popover=[[UIPopoverController alloc]initWithContentViewController:menuVc];
            popover.popoverContentSize=CGSizeMake(200, 300);
            [popover presentPopoverFromRect:CGRectMake(0, 10, 10, 10) inView:listBtn permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];
        }];
}

-(void)removeRightTableView
{
    [rightLabels removeFromSuperview];
    [xiadanBtn removeFromSuperview];
    [listBtn removeFromSuperview];
    [factotyBtn removeFromSuperview];
}



-(void)setupLeftTableView
{
    ProductModel *model = [self.productList objectAtIndex:self.currPage];
    LKDBHelper *helper  = [LKDBHelper getUsingLKDBHelper];
    
    
    NSMutableArray *productInfoArray = [helper search:[ProductInfoModel class] where:@{@"productId":[ToolUtils numberToStr:model.productId]} orderBy:nil offset:0 count:0];
    
    
    NSDictionary *sizeColor=[AppService getSizeAndColorByModel:model.modelNo];
    NSArray *colorList=[sizeColor objectForKey:@"color"];
    NSArray *sizeList=[sizeColor objectForKey:@"size"];

    

    int width = 320.0/([sizeList count]+1);
    int defaultColumnsWidth = 80;
    defaultColumnsWidth = width>defaultColumnsWidth? defaultColumnsWidth:width;
    
    NSMutableArray *colums = [NSMutableArray array];
    for (int i=0;i<[sizeList count];i++) {
        [colums addObject:[NSString stringWithFormat:@"%d",defaultColumnsWidth]];
    }
    [colums addObject:[NSString stringWithFormat:@"%d",defaultColumnsWidth]];
    
    
    labels = [[ProductDetailsLabels alloc]
              initWithFrame:CGRectMake(marginSize, CGRectGetMaxY(_backBtn.frame)+marginSize*2, 100, 40) andColumnsWidths:colums];
    [labels addTopView:model.modelNo];
    NSMutableArray *titleColums = [NSMutableArray array];
    for (int i=0;i<[sizeList count];i++) {
        NSString *size = [sizeList objectAtIndex:i];
        if(i==0)
        {
            [titleColums addObject:@"颜色"];
        }
        [titleColums addObject:size];
    }
    [labels addRecord:titleColums];
    
    

    for (int i=0; i<colorList.count; i++) {
        NSMutableArray *numberColums = [NSMutableArray array];
        NSString *color = [colorList objectAtIndex:i];
        [numberColums addObject:color];
        for (int j=0; j<sizeList.count; j++) {
            NSString *size = [sizeList objectAtIndex:j];
            int number = 0;
            for (int z=0; z<productInfoArray.count; z++) {
                ProductInfoModel *model = [productInfoArray objectAtIndex:z];
                if([model.colorName isEqualToString:color] && [model.sizeName isEqualToString:size])
                {
                    number+=model.productNumber;
                }
            }
            [numberColums addObject:[NSString stringWithFormat:@"%d",number]];
        
        }
        

        [labels addRecord:numberColums];
       
    }

    [labels addBootomView:[NSString stringWithFormat:@"￥ %0.1f 存:%0.0f",model.salePrice,model.totalNumber]];
    [self.view addSubview:labels];
    
    kucunBtn = [[UIButton alloc] initWithFrame:CGRectMake(marginSize, CGRectGetMaxY(labels.frame)+marginSize, 100, 40)];
    [kucunBtn setImage:[UIImage imageNamed:@"kucun_1"] forState:UIControlStateNormal];
    [kucunBtn setImage:[UIImage imageNamed:@"kucun_2"] forState:UIControlStateSelected];
    [self.view addSubview:kucunBtn];
    
    
    [kucunBtn bk_whenTapped:^{
        kucunBtn.selected = !kucunBtn.selected;
        if(kucunBtn.selected)
        {
            labels.hidden = YES;
            kucunBtn.frame = CGRectMake(kucunBtn.frame.origin.x,  CGRectGetMaxY(_backBtn.frame)+marginSize*2, kucunBtn.frame.size.width,  kucunBtn.frame.size.height);
        }else
        {
            labels.hidden = NO;
            kucunBtn.frame = CGRectMake(kucunBtn.frame.origin.x, CGRectGetMaxY(labels.frame)+marginSize, kucunBtn.frame.size.width,  kucunBtn.frame.size.height);
        }
    }];
    

}

-(void)removeLeftTableView
{
    if(labels)
        [labels removeFromSuperview];
    if(kucunBtn)
        [kucunBtn removeFromSuperview];
}




-(void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}



-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}



/*
 *  控制器准备
 */
-(void)vcPrepare{
    
    //去除自动处理
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    //每页准备
    [self pagesPrepare];
    
    //间距
    _scrollViewRightMarginC.constant = - PBMargin;
}



/** 每页准备 */
-(void)pagesPrepare{

    __block CGRect frame = [UIScreen mainScreen].bounds;
    
    CGFloat widthEachPage = frame.size.width + PBMargin;

    //展示页码对应的页面
    [self showWithPage:self.index];
    
    //设置contentSize
    self.scrollView.contentSize = CGSizeMake(widthEachPage * self.photoModels.count, 0);

    self.scrollView.index = _index;
}



/*
 *  展示页码对应的页面
 */
-(void)showWithPage:(NSUInteger)page{
    
   
    
    //如果对应页码对应的视图正在显示中，就不用再显示了
    if([self.visiblePhotoItemViewDictM objectForKey:@(page)] != nil) return;
    
    //取出重用photoItemView
    PhotoItemView *photoItemView = [self dequeReusablePhotoItemView];
    
    if(photoItemView == nil){//没有取到
        
        //重新创建
        photoItemView = [PhotoItemView viewFromXIB];
    }
    
    NSLog(@"%p",&photoItemView);
    
    //数据覆盖
    photoItemView.ItemViewSingleTapBlock = ^(){
        [self singleTap];
    };
    
    //到这里，photoItemView一定有值，而且一定显示为当前页
    //加入到当前显示中的字典
    [self.visiblePhotoItemViewDictM setObject:photoItemView forKey:@(page)];
    
    //传递数据
    //设置页标
    photoItemView.pageIndex = page;
    photoItemView.type = self.type;
    photoItemView.photoModel = self.photoModels[page];

    [self.scrollView addSubview:photoItemView];
    
    //    [UIView animateWithDuration:.01 animations:^{
    photoItemView.alpha=1;
    //    }];
    
    //这里有一个奇怪的重影bug，必须这样解决

//    photoItemView.hidden=YES;
//    [UIView animateWithDuration:.01 animations:^{
//        photoItemView.alpha=1;
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.001 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//            photoItemView.hidden=NO;
//        });
//    
//    }];
}




-(void)singleTap{
    
//    CGFloat h = _topBarView.frame.size.height;
//    
//    BOOL show = _topBarView.tag == 0;
//    
//    _topBarView.tag = show?1:0;
//    
//    _topBarHeightC.constant = show?-h:0;
    
//    [UIView animateWithDuration:.25f animations:^{
//        
//        [_topBarView setNeedsLayout];
//        [_topBarView layoutIfNeeded];
//    }];
    
    [self.scrollView.subviews enumerateObjectsUsingBlock:^(UIView *subView, NSUInteger idx, BOOL *stop) {
        
        if([subView isKindOfClass:[PhotoItemView class]]){
            
            PhotoItemView *itemView = (PhotoItemView *)subView;
            
            [itemView handleBotoomView];
        }
    }];
}




/*
 *  scrollView代理方法区
 */
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    NSUInteger page = [self pageCalWithScrollView:scrollView];
    
    //记录dragPage
    if(self.dragPage == 0) self.dragPage = page;
    
    self.page = page;
    
    CGFloat offsetX = scrollView.contentOffset.x;
    
    CGFloat pageOffsetX = self.dragPage * scrollView.bounds.size.width;
    
    
    if(offsetX > pageOffsetX){//正在向左滑动，展示右边的页面
        
        if(page >= self.pageCount - 1) return;
        
        self.nextPage = page + 1;
        
    }else if(offsetX < pageOffsetX){//正在向右滑动，展示左边的页面
        
        if(page == 0) return;
        
        self.nextPage = page - 1;
    }
}




-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    NSUInteger page = [self pageCalWithScrollView:scrollView];
    
    [self reuserAndVisibleHandle:page];
    self.currPage = page;
    [self removeLeftTableView];
    [self removeRightTableView];
    [self setupLeftTableView];
    [self setupRightTableView];
}



-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    
    //重围
    self.dragPage = 0;
}



-(void)reuserAndVisibleHandle:(NSUInteger)page{
    
    //遍历可视视图字典，除了page之外的所有视图全部移除，并加入重用集合
    [self.visiblePhotoItemViewDictM enumerateKeysAndObjectsUsingBlock:^(NSValue *key, PhotoItemView *photoItemView, BOOL *stop) {
        
        if(![key isEqualToValue:@(page)]){
            
            photoItemView.zoomScale=1;
            
            photoItemView.alpha=0;
            
            [self.reusablePhotoItemViewSetM addObject:photoItemView];
            
            [self.visiblePhotoItemViewDictM removeObjectForKey:key];
        }
    }];
}






-(NSUInteger)pageCalWithScrollView:(UIScrollView *)scrollView{
    
    NSUInteger page = scrollView.contentOffset.x / scrollView.bounds.size.width + .5f;
    
    return page;
}






-(void)setPhotoModels:(NSArray *)photoModels{
    
    _photoModels = photoModels;
    
    self.pageCount = photoModels.count;
    
    //设置源frame标记，以获得动画效果
    PhotoModel *sourcePhotoModel= photoModels[_index];
    sourcePhotoModel.isFromSourceFrame = YES;
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.3f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //初始化页码信息
        self.page = _index;
    });
}



-(void)setPage:(NSUInteger)page{
    
    if(_page !=0 && _page == page) return;
    
    _lastPage = page;
    
    _page = page;
    
    //设置标题
//    NSString *text = [NSString stringWithFormat:@"%@ / %@", @(page + 1) , @(self.pageCount)];
//    
//    dispatch_async(dispatch_get_main_queue(), ^{
//    
//        self.topBarLabel.text = text;
//        [self.topBarLabel setNeedsLayout];
//        [self.topBarLabel layoutIfNeeded];
//    });
    
    
    dispatch_async(dispatch_get_main_queue(), ^{
        
        //显示对应的页面
        [self showWithPage:page];
        
        //获取当前显示中的photoItemView
        self.currentItemView = [self.visiblePhotoItemViewDictM objectForKey:@(self.page)];
    });
}






-(UIStatusBarStyle)preferredStatusBarStyle{
 
    return UIStatusBarStyleLightContent;
}


- (IBAction)leftBtnClick:(id)sender {
    
    [self dismiss];
}

- (IBAction)rightBtnClick:(id)sender {
    
    //取出itemView
    PhotoItemView *itemView = self.currentItemView;
    
    //取出对应模型
    PhotoModel *itemModel = (PhotoModel *)self.photoModels[self.page];
    
    if(!itemView.hasImage){
        [CoreSVP showSVPWithType:CoreSVPTypeError Msg:@"无图片数据" duration:1.0f allowEdit:NO beginBlock:nil completeBlock:nil];
        return;
    }
    
    //读取缓存
    if([itemModel read]){//已经保存过本地
        
        [CoreSVP showSVPWithType:CoreSVPTypeInfo Msg:@"图片已存" duration:1.0f allowEdit:NO beginBlock:nil completeBlock:nil];
    }else{
        
        //展示提示框
        [CoreSVP showSVPWithType:CoreSVPTypeLoadingInterface Msg:@"保存中" duration:0 allowEdit:NO beginBlock:nil completeBlock:nil];
        
        [itemView save:^{
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                [CoreSVP showSVPWithType:CoreSVPTypeSuccess Msg:@"保存成功" duration:1.0f allowEdit:NO beginBlock:nil completeBlock:nil];
                
                //保存记录
                [itemModel save];
            });
            
            
        } failBlock:^{
            
            [CoreSVP showSVPWithType:CoreSVPTypeError Msg:@"保存失败" duration:1.0f allowEdit:NO beginBlock:nil completeBlock:nil];
        }];
    }
}


-(void)setIndex:(NSUInteger)index{
    _index = index ;
}



/** 取出可重用照片视图 */
-(PhotoItemView *)dequeReusablePhotoItemView{
    
    PhotoItemView *photoItemView = [self.reusablePhotoItemViewSetM anyObject];
    
    if(photoItemView != nil){
        
        //从可重用集合中移除
        [self.reusablePhotoItemViewSetM removeObject:photoItemView];
        
        [photoItemView reset];
    }
    
    return photoItemView;
}





/** 可重用集合 */
-(NSMutableSet *)reusablePhotoItemViewSetM{
    
    if(_reusablePhotoItemViewSetM ==nil){
        _reusablePhotoItemViewSetM =[NSMutableSet set];
    }
    
    return _reusablePhotoItemViewSetM;
}


/** 显示中视图字典 */
-(NSMutableDictionary *)visiblePhotoItemViewDictM{
    
    if(_visiblePhotoItemViewDictM == nil){
        
        _visiblePhotoItemViewDictM = [NSMutableDictionary dictionary];
    }
    
    return _visiblePhotoItemViewDictM;
}


-(void)setNextPage:(NSUInteger)nextPage{
    
    if(_nextPage == nextPage) return;
    
    _nextPage = nextPage;
    
    [self showWithPage:nextPage];
}




-(void)dismiss{
    
    
    
    switch (_type) {
        case PhotoBroswerVCTypePush://push

            [self.navigationController popViewControllerAnimated:YES];
            break;
        case PhotoBroswerVCTypeModal://modal
            [self dismissViewControllerAnimated:YES completion:nil];
            break;
            
        case PhotoBroswerVCTypeTransition://transition
            
            [_handleVC.navigationController.view.layer transitionWithAnimType:TransitionAnimTypePageUnCurl subType:TransitionSubtypesFromRamdom curve:TransitionCurveRamdom duration:2.0f];
            [self.navigationController popViewControllerAnimated:NO];
            break;
            
        case PhotoBroswerVCTypeZoom://zoom
        {
            self.view.backgroundColor = [UIColor clearColor];
            
            if(self.currentItemView.zoomScale>1){
                
                self.currentItemView.zoomScale=1;
                self.view.userInteractionEnabled = NO;
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    self.view.userInteractionEnabled = YES;
                    [self zoomOut];
                });
                
            }else{
                
                [self zoomOut];
            }
        }

            break;
            
        default:
            break;
    }
}


-(void)zoomOut{
    
    
    PhotoModel *photoModel = self.photoModels[self.page];
    
    CGRect sourceF = photoModel.sourceFrame;
    
    //检查sourceF是否在屏幕里面
    CGRect screenF =[UIScreen mainScreen].bounds;
    
    BOOL isInScreen = CGRectIntersectsRect(sourceF, screenF);
    
    if(self.currentItemView == nil) isInScreen = NO;
    
    if(isInScreen){
        
        NSLog(@"currentItemView:%@",self.currentItemView);
        
        [self.currentItemView zoomDismiss:^{
            
            [self zoomOutHandle];
        }];
        
    }else{
        
        [UIView animateWithDuration:.5f animations:^{
            
            self.view.transform= CGAffineTransformMakeScale(1.2, 1.2);
            self.view.alpha = 0;
            
        } completion:^(BOOL finished) {
            
            [self zoomOutHandle];
        }];
    }
}


-(void)zoomOutHandle{
    
    _handleVC =nil;
    
    self.photoModels = nil;
    
    //移除视图
    [self.view removeFromSuperview];
    
    self.view = nil;
    
    //移除
    [self removeFromParentViewController];
}


// --自定义的回掉方法
-(void)factoryOrderingClose:(id)sender
{
    [self removeRightTableView];
    [self setupRightTableView];
}



@end
